package com.payment.ktb.activity.main4;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.StringUtils;
import com.payment.ktb.utils.ToastUtils;

import org.apache.http.util.TextUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/12/1.
 */

public class SaleStatisticsActivity extends BaseActivity{


    @BindView(R.id.tv_salestatisticsactivity_timestart)
    TextView tv_salestatisticsactivity_timestart;
    @BindView(R.id.tv_salestatisticsactivity_timeend)
    TextView tv_salestatisticsactivity_timeend;
    @BindView(R.id.et_salestatisticsactivity_agentname)
    EditText et_salestatisticsactivity_agentname;
    @BindView(R.id.btn_salestatisticsactivity_research)
    Button btn_salestatisticsactivity_research;
    @BindView(R.id.tv_salestatisticsactivity_sumProfit)
    TextView tv_salestatisticsactivity_sumProfit;
    @BindView(R.id.tv_salestatisticsactivity_payPrice)
    TextView tv_salestatisticsactivity_payPrice;
    @BindView(R.id.tv_salestatisticsactivity_allProfit)
    TextView tv_salestatisticsactivity_allProfit;
    @BindView(R.id.tv_salestatisticsactivity_title)
    TextView tv_salestatisticsactivity_title;
    @BindView(R.id.ll_salestatistics_detail)
    LinearLayout ll_salestatistics_detail;
    @BindView(R.id.rl_salestatistics_noagent)
    RelativeLayout rl_salestatistics_noagent;




    String ts = new SimpleDateFormat("yyyy-MM").format(new Date()) + "-01 00:00:00";
    String te = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " 23:59:59";
    private String orgName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_salestatistics);
        ButterKnife.bind(this);
        initTitle("销售统计");
        initData();
        getSaleStatistics();
    }

    private void initData() {
        tv_salestatisticsactivity_timestart.setText(new SimpleDateFormat("yyyy-MM").format(new Date()) + "-01");
        tv_salestatisticsactivity_timeend.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

    }


    @OnClick({R.id.btn_salestatisticsactivity_research,R.id.tv_salestatisticsactivity_timestart,R.id.tv_salestatisticsactivity_timeend})
    public void clickEvent(View view) {

        switch (view.getId()) {

            //搜索代理商
            case R.id.btn_salestatisticsactivity_research:

                orgName = et_salestatisticsactivity_agentname.getText().toString().trim();
                getSaleStatistics();
                break;


            //开始时间
            case R.id.tv_salestatisticsactivity_timestart:
                showStartDateDialog();
                break;

            //结束时间
            case R.id.tv_salestatisticsactivity_timeend:
                showEndDateDialog();
                break;


        }

    }









    /**
     *
     */
    private void showStartDateDialog() {

        ToastUtils.showToast("请选择开始时间！");
        new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                String time = String.format("%d-%d-%d",year,monthOfYear + 1,dayOfMonth);
                ts = time + " 00:00:00";
                tv_salestatisticsactivity_timestart.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }
        }, Calendar.getInstance().get(Calendar.YEAR),Calendar.getInstance().get(Calendar.MONTH),Calendar.getInstance().get(Calendar.DAY_OF_MONTH)).show();
    }



    /**
     *
     */
    private void showEndDateDialog() {

        ToastUtils.showToast("请选择结束时间！");
        new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                String time = String.format("%d-%d-%d",year,monthOfYear + 1,dayOfMonth);
                te = time + " 23:59:59";
                tv_salestatisticsactivity_timeend.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }
        }, Calendar.getInstance().get(Calendar.YEAR),Calendar.getInstance().get(Calendar.MONTH),Calendar.getInstance().get(Calendar.DAY_OF_MONTH)).show();
    }









    public void getSaleStatistics() {



        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("ts",ts);
        map.put("te",te);
        map.put("orgName",orgName);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgChildSts, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {

                    ll_salestatistics_detail.setVisibility(View.VISIBLE);
                    rl_salestatistics_noagent.setVisibility(View.GONE);
                    JSONObject jsonObject = new JSONObject(response);
                    tv_salestatisticsactivity_title.setText(jsonObject.optString("showTile"));
                    tv_salestatisticsactivity_sumProfit.setText(StringUtils.handleFen2Yuan(jsonObject.optString("sumProfit")));
                    tv_salestatisticsactivity_payPrice.setText("共" + jsonObject.optString("payCount") + "笔 " + StringUtils.handleFen2Yuan(jsonObject.optString("payPrice")));
                    tv_salestatisticsactivity_allProfit.setText(StringUtils.handleFen2Yuan(jsonObject.optString("allProfit")));




                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();

                if (volleyError.getMessage().equals("找不到该代理商")) {
                    ll_salestatistics_detail.setVisibility(View.GONE);
                    rl_salestatistics_noagent.setVisibility(View.VISIBLE);
                }else {
                    AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
                }
            }
        });

    }
}
